<template>
  <el-dialog :visible.sync="visible" class="custom-dialog" :close-on-click-modal="false" :destroy-on-close="true"
    :modal="true" width="720px" :before-close="beforeClose" append-to-body>
    <template slot="title" append-to-body>
      <div class="header">
        <div class="title">选择专业</div>
      </div>
    </template>
    <div class="container">
      <div class="left">
        <el-menu :default-active="currentSecondNavIndex" :collapse="false" :unique-opened="true" ref="menuRef"
          @select="selectMenu" @open="fatherMenuOpen">
          <el-submenu v-for="item,index in classList" :key="item.depId" :index="index+''" :title="item.depName">
            <template slot="title">
              <div class="titleFirst">{{ item.depName }}</div>
            </template>
            <template v-if="item.children.length > 0">
              <el-menu-item v-for="temp,tempIndex in item.children" :key="temp.depId" :index="tempIndex+''"
                :title="temp.depName" :id="'menu' + temp.depId">{{ temp.depName }}
              </el-menu-item>
            </template>
          </el-submenu>
        </el-menu>
      </div>
      <div class="right" v-loading="loading">
        <div class="right-item" v-if="thirdListObj.depName">
          <div class="right-item-title">
            <span class="title">{{ thirdListObj.depName }}</span>
          </div>
          <div class="right-item-checkbox">
            <el-checkbox v-for="_item in thirdListObj.children" :key="_item.depId" :label="_item.depName"
              v-model="_item.checked" @change="(val) => handleChangeCheck(val, _item)"></el-checkbox>
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { positionTreeInterface } from "@/api/home.js";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    visible: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val) {
          this.getClassList();
        }
      },
    },
  },
  data() {
    return {
      loading: false,
      currentFirstNavIndex: "", // 当前点击的左侧菜单一级 depId
      currentSecondNavIndex: "", // 当前点击的左侧菜单二级 depId
      classList: [], // 左侧数据列表
      thirdListObj: {}, // 点击左侧菜单的当前二级数据(包含三级数据)对象
      selectedList: [], // 当前选中的列表，包括从父组件传过来的数据和当前选中的数据
      selectedMap: {}, // 当前选中的 depId 的map；
      listMap: {}, // 请求过的数据就不去请求了，直接在这里获取
    };
  },
  created() {},
  methods: {
    fatherMenuOpen(key) {
      this.selectMenu("0", [key, "0"]);
    },
    //参数的值里面的数字必须是字符串数字！！
    selectMenu(index, indexPath) {
      // this.currentFirstNavIndex = indexPath[0];
      // this.currentSecondNavIndex = indexPath[1];
      // console.log(currentFirstNavIndex, currentSecondNavIndex);

      // 如果在map中已经存在数据，就在map中获取数据，不去请求
      this.thirdListObj =
        this.classList[indexPath[0]]["children"][indexPath[1]];
    },

    handleChangeCheck(val, item) {
      if (val) {
        //因为是单选 所有每次都要清空
        this.selectedList = [];
        // 每次选中，添加进去选中列表和map中
        if (!this.selectedMap[item.depId]) {
          this.selectedMap[item.depId] = {
            checked: true, // checked是当前弹框中选中职业的标识
            isSelected: false, // 这个字段是多选时候选中的标识，是父组件用到的字段
          };
          this.selectedList.push(item);
        }
      } else {
        // 每次反选，就从选中列表和map中清除
        delete this.selectedMap[item.depId];
        const index = this.selectedList.findIndex(
          (_item) => _item.depId === item.depId
        );
        this.selectedList.splice(index, 1);
      }
      this.$emit("confirm", this.selectedList);
      this.beforeClose();
    },
    handleClose() {
      this.currentFirstNavIndex = "";
      this.selectedMap = {};
      this.thirdListObj = {};
      this.classList = [];
      this.$emit("update:visible", false);
    },
    beforeClose() {
      this.handleClose();
    },
    getClassList() {
      positionTreeInterface({
        // careerTagIds: "",
      })
        .then((res) => {
          if (res.code == 200 && Array.isArray(res.data)) {
            res.data.forEach((item) => {
              this.$set(item, "checked", false);
            });
            this.classList = res.data;
            this.currentFirstNavIndex = "0";
            this.currentSecondNavIndex = "0";
            this.selectMenu(this.currentSecondNavIndex, [
              this.currentFirstNavIndex,
              this.currentSecondNavIndex,
            ]);
          }
        })
        .catch((e) => {
          this.$message.error("获取专业列表失败，请重试！");
          setTimeout(() => {
            this.beforeClose();
          }, 1500);
        });
    },
  },
};
</script>

<style scoped lang="scss">
$main-color: #0b6eff;
.header {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #c8d4e6;
  padding-bottom: 12px;
  .title {
    font-size: 18px;
    color: #222;
    margin: 4px 14px 0 16px;
  }
  .custom-input {
    width: 313px;
    ::v-deep .el-input__inner {
      background: #e8e8e8;
    }
  }
}
.container {
  height: 480px;
  display: flex;
  font-size: 14px;
  .left {
    padding: 8px 0;
    height: 100%;
    width: 176px;
    flex-shrink: 0;
    border-right: 1px solid #c8d4e6;
    max-height: 480px;
    overflow-y: auto;
    overflow-x: hidden;
    &::-webkit-scrollbar-track-piece {
      background: #fff;
    }

    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-thumb {
      background: #d8d8d8;
      border-radius: 20px;
    }
    ::v-deep .el-submenu__title {
      height: 36px;
      line-height: 36px;
      font-size: 14px;
      padding-right: 0px;
    }
    ::v-deep .el-menu-item {
      height: 36px;
      line-height: 36px;
      padding-left: 34px !important;
    }
    ::v-deep .el-submenu__icon-arrow {
      right: 8px;
    }
  }
  .all {
    width: 50px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    border: 1px solid #9ea9c1;
    border-radius: 3px;
    margin-left: 12px;
    cursor: pointer;
    user-select: none;
    font-size: 13px;
  }
  .right {
    flex: 1;
    padding: 8px 0 12px 18px;
    &-item {
      color: #555;
      &-title {
        display: flex;
        align-items: center;
        padding: 4px 0 15px 0;
        &::before {
          content: " ";
          width: 3px;
          height: 13px;
          background-color: $main-color;
          border-radius: 2px;
          transform: translateY(1px);
        }
        .title {
          font-size: 16px;
          padding-left: 8px;
          padding-top: 3px;
          font-weight: 600;
        }
        .active {
          background: $main-color;
          color: #fff;
          border-color: $main-color;
        }
      }
      &-checkbox {
        margin-bottom: 6px;
      }
    }
    &-search {
      &-title {
        display: flex;
        align-items: center;
        padding: 4px 0 12px 0;
        .title-box {
          color: #999;
          display: flex;
          align-items: center;
          font-size: 13px;
          display: flex;
          align-items: center;
          padding-top: 4px;
          .dot {
            padding: 0 4px;
            font-size: 12px;
            padding-top: 1px;
          }
          .active {
            background: $main-color;
            color: #fff;
            border-color: $main-color;
          }
        }
      }
    }
  }
}
::v-deep .el-input__inner::placeholder {
  color: #a5a5a5;
}
::v-deep .el-dialog__headerbtn {
  top: 16px;
  right: 16px;
}
::v-deep .el-icon-close:before {
  font-size: 22px;
  color: #000;
}
::v-deep .el-icon-search:before {
  color: #222;
}
::v-deep .el-dialog__wrapper {
  z-index: 999999 !important; // 因为会被预览文档的层级挡住弹框的点击
}
::v-deep .el-dialog {
  border-radius: 8px;
  z-index: 3000 !important;
}
::v-deep .el-dialog__header {
  padding: 12px 0 0 0;
}
::v-deep .el-dialog__body {
  padding: 0;
  z-index: 3000 !important;
}

::v-deep .el-checkbox {
  margin: 0 40px 16px 0;
  .el-checkbox__inner {
    border-radius: 3px;
    background: transparent;
    border-color: #9c9c9c;
  }
  .el-checkbox__input.is-checked .el-checkbox__inner {
    border-color: $main-color;
  }
  .el-checkbox__inner::after {
    border: 1px solid $main-color;
    border-left: 0;
    border-top: 0;
  }
}
</style>
